//
//  ViewController.swift
//  hotSearchForSwift
//
//  Created by Xiang on 2021/5/15.
//

import UIKit
import Foundation

class ViewController: UIViewController {

    lazy var titles : NSMutableArray = ["苹果","apple","桃子","peach","梨子","pear","橘子","orange","葡萄","grape","花生","peanut","香蕉","banana","咖啡","coffee"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupLayoutViews()

    }
    
    func setupLayoutViews () {
        
        var x : CGFloat = 15
        var y : CGFloat = 10 + 40
        
        for i in 0..<titles.count {
            
            let label = labelWithTitle(title: titles[i] as! String)
            let width : CGFloat = label.frame.size.width+30
            
            if (x+width+15 > view.frame.size.width) {
                y += 40
                x = 15
            }
            
            label.frame = CGRect(x: x, y: y, width: width, height: 30)
            view.addSubview(label)
            x += width + 10
        }
    }
    
    func labelWithTitle(title : String) -> UILabel {
        let label = UILabel()
        label.text = title
        label.sizeToFit()
        label.layer.cornerRadius = 3
        label.layer.borderWidth = 0.5
        label.isUserInteractionEnabled = true
        label.font = UIFont.systemFont(ofSize: 13)
        label.textAlignment = NSTextAlignment.center
        return label
    }
    
}

